import { useEffect, useMemo, useState } from 'react';
import { connect, history } from 'umi';
import { Button, Spin, Tabs, message, Row, Col, Menu, Form } from 'antd';
import Cls from 'classnames';
import '../common/styles.less';
import pageStyles from '@/common/pageLayout.less';
import { IntentTableBody } from './IntentTableBody';
import { taskPushService } from '../services';
const taskCenter: React.FC<ContractHomesAPI.IPurchaseHomeView> = ({ curRes }) => {
  /** 样式根目录 */
  const styleFixCls = 'purchase-homesty';
  // 搜索表单
  const [searchForm] = Form.useForm();
  /** 保存数据 */
  const [manageDataList, setManageDataList] = useState<Record<string, any>[]>([]);

  const [listData, setListData]: any = useState();

  /** 回调 */
  const homeCallback = (res: any) => {
    // console.log(res)
    if (res && res?.msgFlag) {
      setManageDataList(res?.rows);
    } else {
      message.error(res?.msg);
    }
  };

  // 资产转移 assetApplyAllot
  // const { taskLoadingIntent, getTaskCheckDataIntent, reloadTaskDataIntent, dataIntent, paginationIntent } = purchaseSourceIntentService(curRes, homeCallback);

  /**获取全部数据 */
  const { taskPushRun, taskPushLoading, taskPushPagination } = taskPushService((res) => {
    if (res?.msgFlag && res?.rows?.length > 0) {
      const tempMap = res?.rows?.map((item) => {
        return {
          ...item,
          currentStage: item?.nodeName, //当前环节， nodeName为react关键字不能使用！！！
        };
      });
      setListData(tempMap);
    } else {
      setListData([]);
    }
  });

  /** 存入选中的tab标识 */
  const [tabPaneKey, setTabPaneKey] = useState<string>('0');

  /**初始化调用获取数据 */
  useEffect(() => {
    taskPushRun({
      isRemark: tabPaneKey,
      current: 1,
      pageSize: 10,
    });
  }, [tabPaneKey]);

  /** 返回子组件 */
  const renderTabsPane = () => {
    if (tabPaneKey == '0' || tabPaneKey == '2') {
      return (
        <IntentTableBody
          taskPushPagination={taskPushPagination}
          tabPaneKey={tabPaneKey}
          listData={listData}
          styleFixCls={styleFixCls}
          searchForm={searchForm}
          isTaskView={true}
          reloadListData={taskPushRun}
        />
      );
    } else {
      return <></>;
    }
  };

  /** tabs子页面 no*/
  const tabsItem = useMemo(() => {
    let items: any[] = [];
    items = [
      {
        label: '待办',
        key: '0',
        children: renderTabsPane(),
        isShow: true,
      },
      {
        label: '已办',
        key: '2',
        children: renderTabsPane(),
        isShow: true,
      },
    ];
    const newItems: any[] = items?.filter((item: any) => item?.isShow);
    return newItems;
  }, [manageDataList, curRes, listData]);

  return (
    <div className={Cls(`${styleFixCls}`, pageStyles['commonTabsLine'])}>
      <Row gutter={[8, 8]} style={{ height: '100%' }}>
        <Col span={24} className={styleFixCls + 'RootDiv-ColSpanA'} style={{ height: '100%' }}>
          <Spin spinning={false}>
            <Tabs
              activeKey={tabPaneKey}
              tabBarExtraContent={{}}
              items={tabsItem}
              onChange={(activeKey: string) => {
                setTabPaneKey(activeKey);
              }}
            />
          </Spin>
        </Col>
      </Row>
    </div>
  );
};

export default connect(({ baseLogin }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
}))(taskCenter);
